﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;

namespace CoolERP_Web.User
{
    [PermissionCheck(false)]
    public partial class SelectWLKC : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFirst();
                BindData();
            }
        }

        private void BindFirst()
        {
            BindWLLX(this.ddlWLLX, "请选择"); 
            BindGYS(this.ddlGYS, "请选择");
            if (Request.QueryString["gid"] != null && !string.IsNullOrEmpty(Request.QueryString["gid"]))
            {
                if (this.ddlGYS.Items.FindByValue(Request.QueryString["gid"]) != null)
                    this.ddlGYS.Items.FindByValue(Request.QueryString["gid"]).Selected = true;
                tdGYS.Visible = false;
            }
            this.AspNetPager1.PageSize = GridView1.PageSize;
        }

        protected void btnQuery_Click(object sender, ImageClickEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            BindData();
        }

        private void BindData()
        {
            Dictionary<string,string> dc=new Dictionary<string,string>();
            if (!string.IsNullOrEmpty(ddlGYS.SelectedValue))
                dc.Add("GYS", ddlGYS.SelectedValue);
            if(!string.IsNullOrEmpty(txtName.Text.Trim()))
                dc.Add("MC",txtName.Text.Trim());
            if (!string.IsNullOrEmpty(ddlWLLX.SelectedValue))
                dc.Add("WLLX", ddlWLLX.SelectedValue);

            using (KCBLL kBLL = new KCBLL())
            {
                int RecordCount = 0;
                List<WLKCModel> itemLists = kBLL.GetWLKC(dc, false, "BH asc", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                this.AspNetPager1.RecordCount = RecordCount;
                GridView1.DataSource = itemLists;
                GridView1.DataBind();
                this.AspNetPager1.Visible = RecordCount > 0;
                panMsg.Visible = !this.AspNetPager1.Visible;
            }
            List<int> cols = new List<int>() { 1, 2, 3, 4, 5, 6, 7 };
            Utility.MergeCols(GridView1, cols);
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }
    }
}